package rsee.com;

import com.video.h264.EyeProtocolParser;

import android.graphics.Bitmap;
import android.util.Log;

public class EyeSourceTrans {
	private String TAG = "EyeSourceTrans";
	// inherit by EyeSourceMgr
	public interface EyeSourceTransCallBack {
		void onUpdateData(SourceIdent ident, EyeFrameData data);
		void onUpdateState(SourceIdent ident, EyeSourceTrans.TransState state, int errCode);
		void onGetStreamInfo(SourceIdent ident, EyeProtocolParser.StreamDataFormat stream_format);
	}
	
	public boolean isNet() {
		boolean result = (this instanceof EyeSourceTransNet);
		return result;
	}
	
	//================ Interface ================
	public enum TransState {
		TransStateErr,
		TransStateWait,
		TransStateUnconn,
		TransStateRun,
		ChangeChanenlSucc,
		ErrVersion,
		ConnectAgain;
	}
	
	private SourceIdent mIdent = null;
	private TransState mState = null;
	private int mErrCode = 0;
	private Bitmap[] bitmapArr= new Bitmap[4];
	
	protected EyeSourceTrans(SourceIdent ident, EyeSourceTransCallBack listener) {
		mIdent = ident;
		mListener = listener;
		mState = TransState.TransStateWait;
		mErrCode = 0;
	}

	public void setBitmap(byte channel,Bitmap bitmap)
	{
		bitmapArr[channel] = bitmap;
	}
	
	public Bitmap getBitmap(byte channel)
	{
		return bitmapArr[channel];
	}
	public SourceIdent getIdent() {
		return mIdent;
	}
	
	public Bitmap getBitmap() {
		// for inherit
		Log.i(TAG, "getBitmap");
		return null;
	}
	

	public RGBBitmap getRGBBitmap() {
		// for inherit
    	return null;
    }
    
	public int getBitmapId()
	{
		// for inherit
		return 0;
	}
	
	public void request() {
		// for inherit
	}
	
	public void close() {
		// for inherit
	}
	
	public TransState getState() {
		return mState;
	}
	
	public void setState(TransState state) {
		mState = state;
	}
	
	public int getErrCode() {
		return mErrCode;
	}
	
	public void setErrCode(int errCode) {
		mErrCode = errCode;
	}
	
	//================ Listener ================
	private EyeSourceTransCallBack mListener = null;
	
	protected EyeSourceTransCallBack getListener() {
		return mListener;
	}
	
	protected void notifyUpdateData(EyeFrameData data) {
		if (null == mListener)
			return;
		
		SourceIdent ident = getIdent();
		mListener.onUpdateData(ident, data);
	}
	
	protected void notifyUpdateState() {
		if (null == mListener)
			return;

		int errCode = getErrCode();
		SourceIdent ident = getIdent();
		mListener.onUpdateState(ident, mState, errCode);
	}
	
	protected void notifyGetStreamInfo(EyeProtocolParser.StreamDataFormat streamInfo) {
        if (null == mListener)
            return;

        SourceIdent ident = getIdent();
        mListener.onGetStreamInfo(ident, streamInfo);
    }
	
	//================ Creator ================
	
	public static EyeSourceTrans createSourceTrans(SourceIdent ident, EyeSourceTransCallBack listener,int  streamMode, int dataType,int Version,byte[] channelGroup) {
		EyeSourceTrans target = null;
		try {
			if (ident instanceof SourceIdentNet) {
				target = new EyeSourceTransNet((SourceIdentNet)ident, listener,  streamMode, dataType,Version,channelGroup);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return target;
	}
}
